package com.pine.admin.modules.system.service.impl;

import com.pine.common.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import com.pine.common.dto.Page;

import com.pine.admin.modules.system.dao.SysUserDao;
import com.pine.admin.modules.system.entity.SysUserEntity;
import com.pine.admin.modules.system.service.SysUserService;


@Service("sysUserService")
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;


    @Override
    public List<SysUserEntity> getSysUserList(SysUserEntity record) {

        log.info("getSysUserList");
        try {

            return sysUserDao.queryAllSysUser(record);
        } catch (Exception e) {
            log.error("getSysUserList", e);
        }

        return null;
    }

    @Override
    public List<SysUserEntity> getSysUserByPage(Page page, SysUserEntity record) {

        log.info("getSysUserByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put("page", page);

            return sysUserDao.querySysUsersByPage(parameter);
        } catch (Exception e) {
            log.error("getSysUserByPage", e);
        }

        return null;
    }

    @Override
    public SysUserEntity getSysUserById(String id) {

        log.info("getSysUserById");
        try {

            return sysUserDao.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            log.error("getSysUserById", e);
        }

        return null;
    }

    @Override
    public SysUserEntity getSysUser(SysUserEntity record) {

        log.info("getSysUser");
        try {

            return sysUserDao.querySysUser(record);
        } catch (Exception e) {
            log.error("getSysUser", e);
        }

        return null;
    }

    @Override
    public boolean createSysUser(SysUserEntity record) {

        log.info("createSysUser");
        try {
            if (record.getPassword() != null){
                record.setPassword(MD5Util.getMD5(record.getPassword()));
            }
            int updates = sysUserDao.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("createSysUser", e);
        }

        return false;
    }

    @Override
    public boolean deleteSysUser(String id) {

        log.info("deleteSysUser");
        try {

            int updates = sysUserDao.delete(Integer.valueOf(id));

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("deleteSysUser", e);
        }

        return false;
    }

    @Override
    public boolean updateSysUser(SysUserEntity record) {

        log.info("updateSysUser");
        try {
            if (record.getPassword()!=null){
                record.setPassword(MD5Util.getMD5(record.getPassword()));
            }
            int updates = sysUserDao.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("updateSysUser", e);
        }

        return false;
    }

}
